<?php

class Admin extends Admin_Controller {

    //put your code here
    function Admin() {
        parent::Admin_Controller();
        $this->load->model('Admin_m');
        $this->load->model('Accueil_m');
        $this->load->helper('date');
        $this->_data['erreur'] = "";
    }

    function index() {
        is_admin();
        $this->list_user(3);
    }

    function login() {
        $data['erreur'] = FALSE;
        $config = array(
            array(
                'field' => 'email',
                'label' => 'email',
                'rules' => 'required|valid_email'
            ),
            array(
                'field' => 'pass',
                'label' => 'password',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $email = $this->input->post('email');
                $password = md5($this->input->post('pass'));
                $query1 = $this->Admin_m->get_deactivate_user($email, $password);
                if ($query1->num_rows > 0) {
                    $aRow = $query1->row();
                    if ($aRow->active == 0) {
                        $data['erreur'] = "Account is banned!!!";
                        $this->load->view('admin/login',$data);
                    } else {
                        $query = $this->Admin_m->login($email, $password);
                        if ($query->num_rows() > 0) {
                            $row = $query->row();
                            $this->session->set_userdata('adminid', $row->id);
                            $this->session->set_userdata('email', $row->email);
                            $this->session->set_userdata('level', $row->group_id);
                            redirect('admin');
                        } else {
                            $data['erreur'] = "Identifiant ou mot de passe n'existe pas";
                            $this->load->view('admin/login',$data);
                        }
                    }
                } else {
                    $data['erreur'] = "Identifiant ou mot de passe n'existe pas";
                    $this->load->view('admin/login',$data);
                }
            } else {
                $this->load->view('admin/login');
            }
        } else {
            $this->load->view('admin/login');
        }
    }

    function check_deactive_user() {
        $email = $this->input->post('email');
        $password = $this->input->post('pass');
        $query = $this->Admin_m->get_deactivate_user($email, md5($password));
        if ($query->num_rows > 0) {
            $aRow = $query->row();
            if ($aRow->active == 0) {
                $this->form_validation->set_message('check_deactive_user', 'Account is banned!!!');
                return FALSE;
            }
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . 'index.php/admin');
    }

    function edit_profile($id=0, $level=0) {
        is_admin();
        $this->_data['level'] = $level;

        $birthday = $this->input->post('birth_year') . '-' . $this->input->post('birth_month') . '-' . $this->input->post('birth_date') . ' ' . '06:00';
        $dob_long = human_to_unix($birthday);
        $admin = $this->Admin_m->get_admin_details($id);
        $this->_data['admin_data'] = $admin;

        $this->_data['erreur'] = "";
        $config = array(
            array(
                'field' => 'display_name',
                'label' => 'Display name',
                'rules' => 'required'
            ),
            array(
                'field' => 'first_name',
                'label' => 'First name',
                'rules' => 'required'
            ),
            array(
                'field' => 'last_name',
                'label' => 'Last name',
                'rules' => 'required'
            ),
            array(
                'field' => 'company',
                'label' => 'Company',
                'rules' => 'required'
            ),
            array(
                'field' => 'address',
                'label' => 'address',
                'rules' => 'required'
            ),
            array(
                'field' => 'phone',
                'Label' => 'Phone number',
                'rules' => 'min_length[10]'
            ),
            array(
                'field' => 'mobile',
                'Label' => 'Mobile number',
                'rules' => 'min_length[10]'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            $image_path = './assets/avatar';
            $thumb_path = $image_path . '/images';
            $config = array(
                'allowed_types' => "jpg|jpeg|gif|png",
                'upload_path' => $image_path,
                'max_size' => 10000,
                'encrypt_name' =>TRUE
            );
            $this->_data['page_title'] = "Uploading...";
            $this->load->library('upload', $config);

            if ($this->upload->do_upload()) {
                $upload_info = $this->upload->data();
                $config = array(
                    'source_image' => $upload_info['full_path'], //get original image
                    'new_image' => $thumb_path, //save as new image //need to create thumbs first
                    'maintain_ratio' => true,
                    'width' => 300,
                    'height' => 200
                );
                $this->load->library('image_lib', $config); //load library
                $this->image_lib->resize(); //do whatever specified in config
                delete_pro_file($upload_info['full_path']);

                if ($this->form_validation->run()) {
                    $data = array(
                        'display_name' => $this->input->post('display_name'),
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'company' => $this->input->post('company'),
                        'gender' => $this->input->post('gender'),
                        'dob' => $dob_long,
                        'phone' => $this->input->post('phone'),
                        'mobile' => $this->input->post('mobile'),
                        'address' => $this->input->post('address'),
                        'gravatar' => $upload_info['file_name']
                    );
                    $this->Admin_m->edit_profile($data, $id);
                    redirect('admin/list_user' . '/' . $level);
                } else {
                    $this->display_view('admin/edit_profile');
                }
            } else {
                if ($this->form_validation->run()) {
                    $data = array(
                        'display_name' => $this->input->post('display_name'),
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'company' => $this->input->post('company'),
                        'gender' => $this->input->post('gender'),
                        'dob' => $dob_long,
                        'phone' => $this->input->post('phone'),
                        'mobile' => $this->input->post('mobile'),
                        'address' => $this->input->post('address')
                    );
                    $this->Admin_m->edit_profile($data, $id);
                    redirect('admin/list_user' . '/' . $level);
                } else {
                    $this->display_view('admin/edit_profile');
                }
            }
        } else {
            $this->display_view('admin/edit_profile');
        }
    }

    function change_pass($id, $level) {
        is_admin();
        $adminid = $this->session->userdata('adminid');
        $this->_data['level'] = $level;
        $this->_data['erreur'] = "";

        $config = array(
            array(
                'field' => 'apass',
                'label' => 'Ancien mot de passe',
                'rules' => 'required|md5'
            ),
            array(
                'field' => 'npass',
                'label' => 'Nouveau mot de passe',
                'rules' => 'required|matches[confirm_npass]|md5'
            ),
            array(
                'field' => 'confirm_npass',
                'label' => 'Confirmation du mot de passe',
                'rules' => 'required|md5'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $pass = $this->input->post('apass');
            $query = $this->Admin_m->is_password($pass, $adminid);
            if ($query) {
                $data = array('password' => $this->input->post('npass'));
                $query = $this->Admin_m->change_pass($data, $adminid);
                redirect('admin/list_user/2');
            } else {
                $this->_data['erreur'] = "Mot de passe incorrect";
                $this->display_view('admin/change_pass');
            }
        }else
            $this->display_view('admin/change_pass');
    }

    function list_user($level) {
        is_admin();
        if ($level == 1) {
            redirect('admin');
        }
        if($level==2) $this->_data['titre'] = "Administrateurs";
        else $this->_data['titre'] = "Utilisateurs";
        $this->_data['user_level'] = $level;
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin/list_user/' . $level . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Admin_m->count_user($level);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Admin_m->get_all_user($config['per_page'], $off_set, $level);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Admin_m->count_user($level);
        $this->_data['page_title'] = "Liste de Admin";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_user');
    }

    function delete_user($id, $level, $per_page, $off_set) {
        is_super_admin();
        $this->_data['user_level'] = $level;
        if ($id == 1) {
            redirect('admin');
        }
        if ($this->session->userdata('level') == 1) {
            $this->Admin_m->delete_admin($id);
        } else {
            $this->_data['erreur'] = "Vous n'avez pas la permission";
        }
        redirect(site_url('admin/list_user' . '/' . $level));
    }

    function deactivate_user($id, $level) {
        is_super_admin();
        if ($id == 1) {
            redirect('admin');
        }
        if ($this->session->userdata('level') == 1) {
            $this->Admin_m->deactivate_user($id);
        } else {
            $this->_data['erreur'] = "Vous n'avez pas la permission";
        }
        redirect(site_url('admin/list_user' . '/' . $level));
    }

    function activate_user($id, $level) {
        is_super_admin();
        if ($id == 1) {
            redirect('admin');
        }
        if ($this->session->userdata('level') == 1) {
            $this->Admin_m->activate_user($id);
        } else {
            $this->_data['erreur'] = "Vous n'avez pas la permission";
        }
        redirect(site_url('admin/list_user' . '/' . $level));
    }
    function add_user($level) {
        is_admin();
        $this->_data['user_level'] = $level;
        if ($this->input->post('register')) {
            $formVal = array(
                array(
                    'field' => 'user_name',
                    'Label' => 'User name',
                    'rules' => 'required|callback_check_exist_username|xss_clean'
                ),
                array(
                    'field' => 'email',
                    'Label' => 'Email',
                    'rules' => 'trim|required|valid_email|callback_check_exist_email|xss_clean'
                ),
                array(
                    'field' => 'pass',
                    'Label' => 'Password',
                    'rules' => 'required|matches[confirm_pass]|min_length[5]'
                ),
                array(
                    'field' => 'confirm_pass',
                    'Label' => 'Confirm Password',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'first_name',
                    'Label' => 'First name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'last_name',
                    'Label' => 'Last name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'display_name',
                    'Label' => 'Display name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'company',
                    'Label' => 'Company name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'phone',
                    'Label' => 'Phone number',
                    'rules' => 'min_length[10]'
                ),
                array(
                    'field' => 'mobile',
                    'Label' => 'Mobile number',
                    'rules' => 'min_length[10]'
                ),
                array(
                    'field' => 'address',
                    'Label' => 'Address',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            if ($this->form_validation->run() === TRUE) {
                $image_path = './assets/avatar';
                $thumb_path = $image_path . '/images';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    delete_pro_file($upload_info['full_path']);

                    $birthday = $this->input->post('birth_year') . '-' . $this->input->post('birth_month') . '-' . $this->input->post('birth_date') . ' ' . '06:00';
                    $dob_long = human_to_unix($birthday);
                    $email = $this->input->post('email');
                    $password = $this->input->post('pass');
                    $confirm_password = $this->input->post('confirm_pass');
                    $created_on = now();
                    $data_user = array('email' => $email,
                        'password' => md5($password),
                        'group_id' => $level,
                        'active' => '1',
                        'created_on' => $created_on,
                        'username' => $this->input->post('user_name')
                    );
                    $this->Accueil_m->register_user($data_user);

                    $query_id_max = $this->Accueil_m->get_user_id_max();
                    $user = $query_id_max->row();
                    $user_id = $user->new_id;
                    $display_name = $this->input->post('display_name');

                    $data_profile = array('user_id' => $user_id,
                        'display_name' => $display_name,
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'company' => $this->input->post('company'),
                        'dob' => $dob_long,
                        'gender' => $this->input->post('gender'),
                        'phone' => $this->input->post('phone'),
                        'mobile' => $this->input->post('mobile'),
                        'address' => $this->input->post('address'),
                        'gravatar' => $upload_info['file_name']
                    );
                    $this->Accueil_m->register_profile($data_profile);

                    $this->session->set_userdata('display_name', $display_name);
                    redirect('admin/list_user' . '/' . $level);
                } else {
                    $birthday = $this->input->post('birth_year') . '-' . $this->input->post('birth_month') . '-' . $this->input->post('birth_date') . ' ' . '06:00';
                    $dob_long = human_to_unix($birthday);
                    $email = $this->input->post('email');
                    $password = $this->input->post('pass');
                    $confirm_password = $this->input->post('confirm_pass');
                    $created_on = now();
                    $data_user = array('email' => $email,
                        'password' => md5($password),
                        'group_id' => $level,
                        'active' => '1',
                        'created_on' => $created_on,
                        'username' => $this->input->post('user_name')
                    );
                    $this->Accueil_m->register_user($data_user);

                    $query_id_max = $this->Accueil_m->get_user_id_max();
                    $user = $query_id_max->row();
                    $user_id = $user->new_id;
                    $display_name = $this->input->post('display_name');

                    $data_profile = array('user_id' => $user_id,
                        'display_name' => $display_name,
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'company' => $this->input->post('company'),
                        'dob' => $dob_long,
                        'gender' => $this->input->post('gender'),
                        'phone' => $this->input->post('phone'),
                        'mobile' => $this->input->post('mobile'),
                        'address' => $this->input->post('address')
                    );
                    $this->Accueil_m->register_profile($data_profile);

                    $this->session->set_userdata('display_name', $display_name);
                    redirect('admin/list_user' . '/' . $level);
                }
            } else {
                $this->display_view('admin/add_user');
            }
        } else {
            $this->display_view('admin/add_user');
        }
    }

    function check_exist_email() {
        $email = $this->input->post('email');
        $query = $this->Accueil_m->get_exist_email($email);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_exist_email', 'The email address was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function check_exist_username() {
        $username = $this->input->post('user_name');
        $query = $this->Accueil_m->get_exist_username($username);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_exist_username', 'The user name was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    //List contacts
    function list_all_contacts() {
        is_admin();
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin/list_all_contacts/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Admin_m->count_all_contacts();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['contacts'] = $this->Admin_m->list_all_contacts($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Admin_m->count_all_contacts();
        $this->_data['page_title'] = "Liste de contacts";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_contacts');
    }

    function read_contact($id) {
        is_admin();
        $this->_data['erreur'] = "";
        $contact = $this->Admin_m->read_contact($id);
        $this->_data['read_contact'] = $contact;

        $this->display_view('admin/read_contact');
    }

    function delete_contacts($id) {
        is_admin();
        $this->Admin_m->delete_contact($id);
        redirect('admin/list_all_contacts');
    }

    function edit_propos() {
        is_admin();
        $this->_data['erreur'] = "";
        $propos = $this->Admin_m->get_propos();
        $this->_data['propos'] = $propos;
        if ($this->input->post('submit')) {
            $data = array('propos' => $this->input->post('propos'));
            $query = $this->Admin_m->edit_propos($data);
            redirect('admin');
        } else {
            $this->display_view('admin/edit_propos');
        }
    }

}

?>
